package com.globant.util.log.reports;

import java.util.ArrayList;
import java.util.List;

import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.support.JdbcDaoSupport;

/**
 * Log Reports implementation.
 * 
 * @author		gonzalo.zarza
 * @version	20130128v1.2
 */

public class LogReportsImpl extends JdbcDaoSupport implements ILogReportsDAO {

	/* 
	================================================================================================
	 Methods
	================================================================================================
	*/	
    /**
     * Default constructor.
     */
    public LogReportsImpl(){ }
    
    /* ------------------------------------------------------------------------------------------ */
    
	@Override
	public void insertNewReport(LogReports report) throws DataAccessException {
		String query =	"INSERT INTO log_status (log_id, report_id, status) VALUES (?, ?, ?)";		
		this.getJdbcTemplate().update(query, new Object[] {report.getLogId(), report.getReportId(), report.getStatus()});
	}
	
	/* ------------------------------------------------------------------------------------------ */
	
	@Override
	public void updateReportStatus(String logId, int reportId, int newStatus) throws DataAccessException {
		String query = "UPDATE log_status SET status = ? WHERE log_id = ? AND report_id = ?";
		this.getJdbcTemplate().update(query, new Object[] {newStatus, logId, reportId});
	}
	
	/* ------------------------------------------------------------------------------------------ */
	
	@Override
	public List<LogReports> getReportsByLogId(String logId) throws DataAccessException {		
		String query = "SELECT * FROM log_status WHERE log_id = ?";
		List<?> resultSet = this.getJdbcTemplate().query(query, new Object[]{logId}, new LogReportsRowMapper()); 
		List<LogReports> reports = new ArrayList<LogReports>();
		
		for (Object item : resultSet){
			reports.add((LogReports) item);
		}
		 		
		return (reports);				
	}

	/* ------------------------------------------------------------------------------------------ */
	
	@Override
	public void deleteReportsByLogId(String logId) throws DataAccessException {
		String query = "DELETE FROM log_status WHERE log_id = ?";
		this.getJdbcTemplate().update(query, new Object[] {logId});		
	}
	
}
